package com.controller;

import com.bean.Accender;
import com.bean.Recoding;
import com.bean.Sender;
import com.bean.User;
import com.service.AccenderService;
import com.service.RecodingService;
import com.service.SenderService;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private AccenderService accenderService;
    @Resource
    private SenderService senderService;

    @Resource
    private RecodingService recodingService;

    @RequestMapping(value = "user/listUser", method = RequestMethod.GET)
    public ModelAndView listUser(ModelAndView modelAndView) {
        List<User> userList = userService.queryUserList();
        List<Accender> accenderList = accenderService.queryAccenderList();
        List<Sender> senderList = senderService.querySenderList();
        List<Recoding> recodingList = recodingService.recodingList();
        modelAndView.addObject("userList", userList);
        modelAndView.addObject("accenderList", accenderList);
        modelAndView.addObject("senderList", senderList);
        modelAndView.addObject("recodingList", recodingList);
        modelAndView.addObject( "recoding", new Recoding());
        modelAndView.setViewName("listUser");
        return modelAndView;
    }

    @RequestMapping(value = "user/backToUser", method = RequestMethod.POST)
    public ModelAndView backToUser(@RequestParam("id") int id, @RequestParam("username") String username, @RequestParam("contact") String contact,
                                   @RequestParam("address") String address, @RequestParam("question") String question, @RequestParam("date") Date date) {
        ModelAndView modelAndView = new ModelAndView();
        userService.backToUser(id, username, contact, address, question, date);
        recodingService.deleteRecodingById(id);
        List<User> userList = userService.queryUserList();
        List<Accender> accenderList = accenderService.queryAccenderList();
        List<Sender> senderList = senderService.querySenderList();
        List<Recoding> recodingList = recodingService.recodingList();
        modelAndView.addObject("userList", userList);
        modelAndView.addObject("accenderList", accenderList);
        modelAndView.addObject("senderList", senderList);
        modelAndView.addObject("recodingList", recodingList);
        modelAndView.addObject("recoding", new Recoding());
        modelAndView.setViewName("listUser");
        return modelAndView;
    }


    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> add(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        int res = userService.addUser(user);
        if (res > 0) {
            map.put("status", "1");
        } else {
            map.put("status", "0");
        }
        return map;
    }



    @RequestMapping("/toAddUser")
    public ModelAndView toAddUserInLocal(){
        List<User> userList = userService.queryUserList();
        List<Recoding> recodingList = recodingService.recodingList();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("userList", userList);
        modelAndView.addObject("recodingList", recodingList);
        modelAndView.setViewName("addUser");
        return modelAndView;
    }
    @RequestMapping (value = "/addUserInLocal",method = RequestMethod.POST)
    public ModelAndView addUserInLocal(@RequestParam("username")String username,@RequestParam("question")String question,
                                       @RequestParam("contact")String contact,@RequestParam("address")String address){
        Date date=new Date();
        userService.addUserInLocal(username,question,contact,address,date);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("redirect:/user/listUser");
        return modelAndView;
    }

}

